package com.yunjijia.service.impl;

import com.yunjijia.mapper.UserMapper;
import com.yunjijia.pojo.User;
import com.yunjijia.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper UserMapper;

//    @Resource
//    private UserRoleDAO userRoleDAO;
//    @Resource
//    private UserDAO userDAO;
//    @Resource
//    private RoleDAO roleDAO;
//    @Resource
//    private PermissionDAO permissionDAO;
//    @Resource
//    private RolePermissionDAO rolePermissionDAO;


    //根据用户id查询所有的角色信息
//    public List<Role> findRoles(Integer id) {
//        UserRoleExample example = new UserRoleExample();
//        example.createCriteria().andUidEqualTo(id);
//        List<UserRoleKey> keyList = userRoleDAO.selectByExample(example);
//        List<Integer> roleIdList = new ArrayList<>(keyList.size());
//        for (UserRoleKey userRoleKey : keyList) {
//            roleIdList.add(userRoleKey.getRoleId());
//        }
//        RoleExample roleExample = new RoleExample();
//        roleExample.createCriteria().andIdIn(roleIdList);
//        return roleDAO.selectByExample(roleExample);
//    }
//    //根据用户的id查询所有权限信息
//    public List<Permission> findPermissions(Integer id) {
//        List<Role> roles = findRoles(id);
//        List<Integer> roleIds = new ArrayList<>(roles.size());
//        for (Role role : roles) {
//            roleIds.add(role.getId());
//        }
//        RolePermissionExample example = new RolePermissionExample();
//        example.createCriteria().andRoleIdIn(roleIds);
//        List<RolePermissionKey> keyList = rolePermissionDAO.selectByExample(example);
//        List<Integer> permissionIdList = new ArrayList<>(keyList.size());
//        for (RolePermissionKey rolePermissionKey : keyList) {
//            permissionIdList.add(rolePermissionKey.getPermissionId());
//        }
//        PermissionExample permissionExample = new PermissionExample();
//        if (permissionIdList.size() != 0) {
//            permissionExample.createCriteria().andIdIn(permissionIdList);
//            return permissionDAO.selectByExample(permissionExample);
//        }
//        return new ArrayList<>();
//    }
//
//    public User findUserById(String uId) {
//        return userDAO.selectByPrimaryKey(Integer.valueOf(uId));
//    }
//
//    @Transactional
//    public int assignDefaultUserRolePermission(User user) {
//        int success1 = userDAO.insert(user);
//        UserRoleKey userRoleKey = new UserRoleKey();
//        userRoleKey.setUid(user.getId());
//        userRoleKey.setRoleId(2);
//        int success2 = userRoleDAO.insert(userRoleKey);
//        return success1 + success2;
//    }

    @Override
    public User findByUsername(String username) {
        return UserMapper.selectByName(username);
    }

    /**
     * 查找所有用户
     *
     * @return 返回用户集合
     */
    @Override
    public List<User> findAllUser() {
        return UserMapper.findAllUser();
    }
}
